﻿using NewRen.Helpers;
using NewRen.Log4net;
using NewRen.Models;
using NewRen.Models.DataModel;
using NewRen.Models.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NewRen.Controllers
{
    public class ProductController : BaseController
    {
        // GET: Product
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        ///  产品展示
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Show(int id)
        {
            var _product = db.Products.Find(id);
            if(_product!=null)
            {
                vProduct product = new vProduct(_product);
                if (CurrentUser != null)
                {
                    try
                    {
                        BrowseRecord browserecord = new BrowseRecord();
                        string url = Request.Url.ToString();
                        browserecord = db.BrowseRecords.Where(br => br.URL == url && br.UserID == CurrentUser.ID).FirstOrDefault();
                        if (browserecord == null)
                        {
                            BrowseRecord addbrowserecord = new BrowseRecord();
                            addbrowserecord.UserID = CurrentUser.ID;
                            addbrowserecord.URL = url;
                            addbrowserecord.Title = "商品展示——" + product.ProductName;
                            addbrowserecord.Time = DateTime.Now;
                            db.BrowseRecords.Add(addbrowserecord);
                            db.SaveChanges();
                        }
                        else
                        {
                            db.BrowseRecords.Remove(browserecord);
                            BrowseRecord updatebrowserecord = new BrowseRecord();
                            updatebrowserecord.UserID = CurrentUser.ID;
                            updatebrowserecord.URL = url;
                            updatebrowserecord.Title = "商品展示——" + product.ProductName;
                            updatebrowserecord.Time = DateTime.Now;
                            db.BrowseRecords.Add(updatebrowserecord);
                            db.SaveChanges();
                        }
                    }
                    catch(Exception ex)
                    {
                        log.Error(new LogContent("产品展示出错", HttpHelper.GetIPAddress()), ex);
                        return Redirect("/Shared/Info?msg=数据异常，请稍后重试");
                    }
                }
                return View(product);
            }
            else
            {
                return Redirect("/Shared/Info?msg=数据异常，请稍后重试");
            }
        }


        /// <summary>
        /// 兑换商品
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="UserID"></param>
        /// <returns></returns>
        [ValidateAntiForgeryToken]
        [HttpPost]
        [Authorize]
        public ActionResult Exchange(int ID, int UserID)
        {
            var result = new ExchangeResult();
            try
            {
                var product = db.Products.Find(ID);
                var user = db.Users.Find(UserID);
                if (user.Integration < product.Integration)
                {
                    result.Statu = "err";
                    result.Msg = "积分不够！";
                    return View("ExchangeResult", result);
                }

                user.Integration = user.Integration - product.Integration;
                string code = Helpers.DateHelper.GetTimeStamp();
                var excode = new ExchangeCode();
                excode.ProductID = ID;
                excode.UserID = UserID;
                excode.Time = DateTime.Now;
                excode.Code = code;
                excode.IsExchange = false;
                db.ExchangeCodes.Add(excode);
                db.SaveChanges();
                result.Statu = "ok";
                result.Msg = "兑换成功，请到指定地方领取！";
                result.Code = code;
                return View("ExchangeResult", result);
            }
            catch(Exception ex)
            {
                result.Statu = "err";
                log.Error(new LogContent("兑换出现错误", HttpHelper.GetIPAddress()), ex);
                result.Msg = "兑换出现错误，请重新兑换！";
                return View("ExchangeResult", result);
            }
        }

    }
}